%% return a avg color of item as a number
% im is the original image, fg_bwlabel is a bwlabel matrix, bd_xy is the selected set of coordinates of the item, n is the nth item
function avg_color = get_avg_color_item(im, fg_bwlabel, bd_xy, n)
    avg_color(3) = double(0);
    %% cut out a bounding box matrix that we care about
    % get the dimensions and x, y starting position of our box
    coords = min(bd_xy);
    start_x = coords(1); start_y = coords(2); clear coords;
    dim = max(bd_xy) - min(bd_xy);
    width = dim(1); height = dim(2); clear dim;
    % crop from fg_bwlabel
    crop = uint8(fg_bwlabel(start_x:start_x+width, start_y:start_y+height));
    % edit crop so only the relevant integer n appears
    numpixels = 0;
    for x = 1:width
        for y = 1:height
            if crop(x, y) == n
                crop(x, y) = 1;
                numpixels = numpixels + 1;
            else
                crop(x,y) = 0;
            end
        end
    end;
    % multiply cropped with the relevant section in im
    im_crop = im(start_x:start_x+width, start_y:start_y+height, 1:3);
    for i = 1:3
        avg_color(i) = sum(sum(crop .* im_crop(:,:,i)))/numpixels;
    end;
end